package com.axdac.mixbind.net;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/4/10 0010.
 */

public class NetInvo implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodstr = "" ;
        HttpRequest request;
        if(method.isAnnotationPresent(HttpRequest.class)) {
            request = method.getAnnotation(HttpRequest.class);
            methodstr = request.method();
        }else {
            return null;
        }
        String para = request.para();
        System.out.println("NETInvo:has:"+para);
        if(args!=null) {
            for (int i = 0; i < args.length; i++) {
                para = para.replace("@" + (i + 1),args[i].toString());
            }
        }
        System.out.println("NETInvo:after-param:"+para);

        if(methodstr.equals("get")){
            String requestUrl = para==null?request.url():request.url()+"?"+para;
            return MyHttpClient.getJsonByInternet(requestUrl);
        }else if(methodstr.equals("post")){
            return MyHttpClient.postDownloadJson(request.url(),para);
        }else {
            return null;
        }
    }

}
